export default {
    // 计算续住的总日期列表
    getAll(begin: string, end: string) {
        const arr1 = begin.split('-')
        const arr2 = end.split('-')
        const arr1_ = new Date()
        const arrTime = []
        arr1_.setUTCFullYear(arr1[0] as any, arr1[1] as any - 1, arr1[2] as any)
        const arr2_ = new Date()
        arr2_.setUTCFullYear(arr2[0] as any, arr2[1] as any - 1, arr2[2] as any)
        const unixDb = arr1_.getTime()
        const unixDe = arr2_.getTime()
        for (let k = unixDb; k <= unixDe;) {
            arrTime.push(this.datetimeparse(k, 'yyyy-MM-dd'))
            k = k + 24 * 60 * 60 * 1000
        }
        return arrTime
    },
    // 时间格式处理
    datetimeparse(timestamp?: any, format?: any, prefix?: any) {
        if (typeof timestamp === 'string') {
            timestamp = Number(timestamp)
        }
        // 转换时区
        const currentZoneTime = new Date(timestamp)
        let currentTimestamp = currentZoneTime.getTime()
        const offsetZone = currentZoneTime.getTimezoneOffset() / 60 // 如果offsetZone>0是西区，西区晚
        let offset = null
        // 客户端时间与服务器时间保持一致，固定北京时间东八区。
        offset = offsetZone + 8
        currentTimestamp = currentTimestamp + offset * 3600 * 1000
        let newtimestamp = null
        if (currentTimestamp) {
            if (currentTimestamp.toString().length === 13) {
                newtimestamp = currentTimestamp.toString()
            } else if (currentTimestamp.toString().length === 10) {
                newtimestamp = currentTimestamp + '000'
            } else {
                newtimestamp = null
            }
        } else {
            newtimestamp = null
        }
        const dateobj = newtimestamp
            ? new Date(parseInt(newtimestamp))
            : new Date()
        const YYYY = dateobj.getFullYear()
        const MM =
            dateobj.getMonth() > 8
                ? dateobj.getMonth() + 1
                : '0' + (dateobj.getMonth() + 1)
        const DD =
            dateobj.getDate() > 9 ? dateobj.getDate() : '0' + dateobj.getDate()
        const HH =
            dateobj.getHours() > 9 ? dateobj.getHours() : '0' + dateobj.getHours()
        const mm =
            dateobj.getMinutes() > 9
                ? dateobj.getMinutes()
                : '0' + dateobj.getMinutes()
        const ss =
            dateobj.getSeconds() > 9
                ? dateobj.getSeconds()
                : '0' + dateobj.getSeconds()
        let output = ''
        let separator = '/'
        if (format) {
            separator = format.match(/-/) ? '-' : '/'
            output += format.match(/yy/i) ? YYYY : ''
            output += format.match(/MM/)
                ? (output.length ? separator : '') + MM
                : ''
            output += format.match(/dd/i)
                ? (output.length ? separator : '') + DD
                : ''
            output += format.match(/hh/i) ? (output.length ? ' ' : '') + HH : ''
            output += format.match(/mm/) ? (output.length ? ':' : '') + mm : ''
            output += format.match(/ss/i) ? (output.length ? ':' : '') + ss : ''
        } else {
            output += YYYY + separator + MM + separator + DD
        }
        output = prefix ? prefix + output : output
        return newtimestamp ? output : ''
    },
    // 把年月日转换成月日
    dateChange(time: string | number | Date) {
        const date = new Date(time)
        var m = date.getMonth() + 1
        var d = date.getDate()
        return this.add0(m) + '-' + this.add0(d)
    },
    add0(m: any) {
        return m < 10 ? '0' + m : m
    },
    // 标准日转化为年月日
    transformTime(date: any, year: any) {
        var d = new Date(date)
        if (year) {
            return d.getFullYear() + '年' + this.add0((d.getMonth() + 1)) + '月' + this.add0(d.getDate()) + '日'
        } else {
            return d.getFullYear() + '-' + this.add0((d.getMonth() + 1)) + '-' + this.add0(d.getDate())
        }
    },
    getNowData(timestamp: any) {
        var date = new Date(timestamp * 1000)// 时间戳若为10位时需*1000
        var Y = date.getFullYear()
        var M = date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
        var D = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate()
        var h = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours()
        var m = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes()
        var s = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds()
        // 星期
        var week = date.getDay()
        return `${Y}-${M}-${D} ` + this.dateToWeek(week) + ` ${h}:${m}:${s}`
    },
    // 把年月日转换成星期几
    dateToWeek(time: string | number | Date) {
        const date = new Date(time)
        const num = date.getDay()
        if (num === 0) {
            return '星期日'
        } else if (num === 1) {
            return '星期一'
        } else if (num === 2) {
            return '星期二'
        } else if (num === 3) {
            return '星期三'
        } else if (num === 4) {
            return '星期四'
        } else if (num === 5) {
            return '星期五'
        } else {
            return '星期六'
        }
    },
    // 获取当前可视高度
    getClientHeight() {
        const clientHeight = window.innerHeight
        return clientHeight
    },
    // 获取当前可视宽度
    getClientWidth() {
        const clientWidth = document.documentElement.clientWidth
        return clientWidth
    },
    // el-tree,如果全选中，只获取全选中的父节点，否则选择子节点的值
    getSimpleCheckedNodes(store: any) {
        // 定义数组
        const checkedNodes: any = []
        // 判断是否为全选，若为全选状态返回被全选的节点，不为全选状态正常返回被选中的节点
        const traverse = function (node: any) {
            const childNodes = node.root ? node.root.childNodes : node.childNodes
            childNodes.forEach((child: any) => {
                if (child.checked) {
                    checkedNodes.push(child.data)
                }
                if (child.indeterminate) {
                    traverse(child)
                }
            })
        }
        traverse(store)
        return checkedNodes
    },
    // 用子级，查出父级
    // 查找所有父节点
    getAllParentArr(list: { [x: string]: any }, id: any) {
        for (const i in list) {
            if (list[i].id === id) {
                // 查询到返回该数组对象
                return [list[i]]
            }
            if (list[i].children) {
                const node: any = this.getAllParentArr(list[i].children, id)
                if (node !== undefined) {
                    // 查询到把父节点连起来
                    return node.concat(list[i])
                }
            }
        }
    },
    // 计算两个日期之间相差多少天多少小时多少分
    diffTime(startDate: any, endDate: any) {
        var diff = endDate.getTime() - startDate.getTime() // 时间差的毫秒数
        // 计算出相差天数
        var days = Math.floor(diff / (24 * 3600 * 1000))
        // 计算出小时数
        var leave1 = diff % (24 * 3600 * 1000)   // 计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000))
        // 计算相差分钟数
        var leave2 = leave1 % (3600 * 1000);        // 计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000))
        // 计算相差秒数
        var leave3 = leave2 % (60 * 1000)      // 计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000)
        var returnStr = ''
        if (seconds > 0) {
            returnStr = seconds + "秒"
        }
        if (minutes > 0) {
            returnStr = minutes + "分" + returnStr
        }
        if (hours > 0) {
            returnStr = hours + "小时" + returnStr
        }
        if (days > 0) {
            returnStr = days + "天" + returnStr
        }
        return returnStr
    },
    // 防抖函数
    debounce(cb:any, delay:any) {
        let timer:any = null
        return function(this: unknown,...args:any){
          if (timer){
            clearTimeout(timer)
          }
          timer = setTimeout(() =>{
            cb.call(this,...args)
          },delay)
        }
    },
    getYear() {
        const dt = new Date()
        const now = dt.getFullYear()
        return ('2009' + '-' + now)
    },
    // 获取url参数
    GetArgsFromHref(sHref: string, sArgName: string) {
        var args = sHref.split("?");
        var retval = "";

        if (args[0] === sHref) {
            return retval;
        }
        var str = args[1];
        args = str.split("&");
        for (var i = 0; i < args.length; i++) {
            str = args[i];
            var arg = str.split("=");
            if (arg.length <= 1) continue;
            if (arg[0] === sArgName) retval = arg[1];
        }
        return retval;
    },
}